package com.maochang.config;


import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

public class JDBCConfig {

    @Value("${prop.driverClass}")
    private  String driver;
    @Value("${prop.url}")
    private String url;
    @Value("${prop.username}")
    private String userName;
    @Value("${prop.password}")
    private String password;

    //数据源对象
    @Bean("dataSource")
    public DataSource getDataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }

    //注册jdbcTemplate模板对象
    @Bean("jbcTemplate")
    public JdbcTemplate getJdbcTemplate(@Autowired DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
    //具名参数映射模板对象
    @Bean("jdbcTemplate2")
    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(@Autowired DataSource dataSource){

        return new NamedParameterJdbcTemplate(dataSource);
    }
}
